// 白天主题
const THEME_DAY = '#ffffff';// body背景颜色
const TEXT_DAY = '#000000';  // 文字颜色
// 晚上主题
const THEME_NIGHT = '#2b2b2b';// body背景颜色
const TEXT_NIGHT = '#ffffff'; // 文字颜色
// 当前主题
let THEME_CURRENT = THEME_DAY;
// 当前文字颜色
let TEXT_CURRENT = TEXT_DAY;
// 当前按钮图标 默认是白天
let isDay = true;
// 内容背景颜色
// 白天
let contentBgColorDay = '#ffffff'
// 黑夜
let contentBgColorNight = '#333333'
// 默认是白天
let contentBgColorCurrent = contentBgColorDay
// 按钮背景图片
let btnBgDay = "url(/blog/img/day.png)";
let btnBgNight = "url(/blog/img/night.png)";
let btnBgCurrent = btnBgDay;


window.onload = function () {
    // 从 sessionStorage 中读取主题
    const storedTheme = sessionStorage.getItem('theme');
    console.log(storedTheme)
    console.log(THEME_CURRENT)
    console.log(TEXT_CURRENT)
    if (storedTheme) {
        const { backgroundColor, textColor } = JSON.parse(storedTheme);
        THEME_CURRENT = backgroundColor;
        TEXT_CURRENT = textColor;
        console.log(THEME_CURRENT+"\t1\t"+TEXT_CURRENT)
        // const clickBtn = document.getElementById("changeThemeBtn");
        if(THEME_CURRENT === THEME_DAY) {
            isDay = true;
            contentBgColorCurrent = contentBgColorDay;
            btnBgCurrent = btnBgDay;
            // clickBtn.style.backgroundImage = "url('/blog/img/day.png')";
        } else {
            isDay = false;
            contentBgColorCurrent = contentBgColorNight;
            btnBgCurrent = btnBgNight;
            // clickBtn.style.backgroundImage = "url('/blog/img/night.png')";
        }
    }
    if(THEME_CURRENT==undefined || TEXT_CURRENT==undefined) { // 背景或文字只要有一个不对，则设置为默认主题，且根据时间换成对应的主题
        // 获取当前时间
        const now = new Date();
        // 获取当前小时
        const hour = now.getHours();
        // 如果当前时间在6:00-18:00内，则是白天，否则是晚上
        if(hour>=6 && hour<= 18){
            THEME_CURRENT = THEME_DAY
            TEXT_CURRENT = TEXT_DAY
            contentBgColorCurrent = contentBgColorDay
        } else {
            THEME_CURRENT = THEME_NIGHT
            TEXT_CURRENT = TEXT_NIGHT
            contentBgColorCurrent = contentBgColorNight
        }
        console.log(THEME_CURRENT+"\t2\t"+TEXT_CURRENT+"\t3\t"+contentBgColorCurrent)
    }
    confirmChange();
}

function changeTheme() {
    console.log('点击切换主题按钮了！')
    const clickBtn = document.getElementById("changeThemeBtn");
    if (THEME_CURRENT === THEME_DAY) {
        THEME_CURRENT = THEME_NIGHT;
        TEXT_CURRENT = TEXT_NIGHT;
        contentBgColorCurrent = contentBgColorNight;
        btnBgCurrent = btnBgNight;
    } else {
        THEME_CURRENT = THEME_DAY;
        TEXT_CURRENT = TEXT_DAY;
        contentBgColorCurrent = contentBgColorDay;
        btnBgCurrent = btnBgDay;
    }
    confirmChange();
    // 存储主题到 sessionStorage
    sessionStorage.setItem('theme', JSON.stringify({ backgroundColor: THEME_CURRENT, textColor: TEXT_CURRENT }));
}

function confirmChange() {
    const clickBtn = document.getElementById("changeThemeBtn");
    clickBtn.style.backgroundImage = btnBgCurrent;
    document.body.style.backgroundColor = THEME_CURRENT;
    document.body.style.color = TEXT_CURRENT;
    const links = document.querySelectorAll('.custom-link');// 获取所有自定义链接元素
    links.forEach(link => {
        link.style.color = TEXT_CURRENT;
    });
    const bgs = document.querySelectorAll('.custom-content-bg')
    console.log("bgs-->"+bgs+"contentBgColorCurrent"+contentBgColorCurrent)
    bgs.forEach(bg => {
        bg.style.backgroundColor = contentBgColorCurrent;
    })
}

document.addEventListener('DOMContentLoaded', () => {
    const navItems = document.querySelectorAll('.navbar li');

    navItems.forEach(item => {
        const tooltipText = item.querySelector('a').innerText;
        item.setAttribute('data-tooltip', tooltipText);
    });
});
